//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "HestonModelHelper.h"
using namespace Cephei::QL::Models::Equity;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Models/CalibrationHelper.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Models;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Equity::CHestonModelHelper::CHestonModelHelper (Cephei::QL::Times::IPeriod^ maturity, Cephei::QL::Times::ICalendar^ calendar, Double s0, Double strikePrice, Cephei::QL::IQuote^ volatility, Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeRate, Cephei::QL::Termstructures::IYieldTermStructure^ dividendYield, Microsoft::FSharp::Core::FSharpOption<QL::Models::CalibrationHelper::CalibrationErrorTypeEnum>^ errorType, Cephei::QL::IPricingEngine^ QL_Pricer) : CCalibrationHelper(CHestonModelHelper::typeid)
{
    CPeriod^ _Cmaturity;
    CCalendar^ _Ccalendar;
    CQuote^ _Cvolatility;
    CYieldTermStructure^ _CriskFreeRate;
    CYieldTermStructure^ _CdividendYield;
    try
    {
#ifdef HANDLE
        _phHestonModelHelper = NULL;
#endif
        _Cmaturity = safe_cast<CPeriod^> (maturity);
        _Cmaturity->Lock();
        QuantLib::Period& _maturity = static_cast<QuantLib::Period&> (_Cmaturity->GetReference ()); 
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::Real _s0 = (QuantLib::Real)ValueHelper::Convert (s0);
        QuantLib::Real _strikePrice = (QuantLib::Real)ValueHelper::Convert (strikePrice);
        _Cvolatility = safe_cast<CQuote^> (volatility);
        _Cvolatility->Lock();
        Handle<QuantLib::Quote>& _volatility = static_cast<Handle<QuantLib::Quote>&> (_Cvolatility->GetHandle ()); 
        _CriskFreeRate = safe_cast<CYieldTermStructure^> (riskFreeRate);
        _CriskFreeRate->Lock();
        Handle<QuantLib::YieldTermStructure>& _riskFreeRate = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CriskFreeRate->GetHandle ()); 
        _CdividendYield = safe_cast<CYieldTermStructure^> (dividendYield);
        _CdividendYield->Lock();
        Handle<QuantLib::YieldTermStructure>& _dividendYield = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdividendYield->GetHandle ()); 
        QuantLib::CalibrationHelper::CalibrationErrorType _errorType = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Models::CalibrationHelper::CalibrationErrorTypeEnum>::IsSome::get (errorType) ? (QuantLib::CalibrationHelper::CalibrationErrorType)errorType->Value : QuantLib::CalibrationHelper::CalibrationErrorType::RelativePriceError); //10
        _ppHestonModelHelper = new boost::shared_ptr<QuantLib::HestonModelHelper> (new QuantLib::HestonModelHelper ( _maturity,  _calendar,  _s0,  _strikePrice,  _volatility,  _riskFreeRate,  _dividendYield,  _errorType ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppHestonModelHelper)->setPricingEngine (_QL_Pricer);
        SetCalibrationHelper (boost::dynamic_pointer_cast<QuantLib::CalibrationHelper> (*_ppHestonModelHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmaturity != nullptr) _Cmaturity->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_Cvolatility != nullptr) _Cvolatility->Unlock();
        if (_CriskFreeRate != nullptr) _CriskFreeRate->Unlock();
        if (_CdividendYield != nullptr) _CdividendYield->Unlock();
    }
}
Cephei::QL::Models::Equity::CHestonModelHelper::CHestonModelHelper (boost::shared_ptr<QuantLib::HestonModelHelper>& childNative, Object^ owner) : CCalibrationHelper(CHestonModelHelper::typeid)
{
#ifdef HANDLE
	_phHestonModelHelper = NULL;
#endif
	_ppHestonModelHelper = &childNative;
    _ppCalibrationHelper = new boost::shared_ptr<QuantLib::CalibrationHelper> (boost::dynamic_pointer_cast<QuantLib::CalibrationHelper> (*_ppHestonModelHelper));
}
Cephei::QL::Models::Equity::CHestonModelHelper::CHestonModelHelper (QuantLib::HestonModelHelper& childNative, Object^ owner) : CCalibrationHelper(CHestonModelHelper::typeid)
{
#ifdef HANDLE
	_phHestonModelHelper = NULL;
#endif
	_ppHestonModelHelper = new boost::shared_ptr<QuantLib::HestonModelHelper> (&childNative);
    _ppCalibrationHelper = new boost::shared_ptr<QuantLib::CalibrationHelper> (boost::dynamic_pointer_cast<QuantLib::CalibrationHelper> (*_ppHestonModelHelper));
    _HestonModelHelperOwner = owner;
    _CalibrationHelperOwner = owner;
}

Cephei::QL::Models::Equity::CHestonModelHelper::CHestonModelHelper (CHestonModelHelper^ copy) : CCalibrationHelper(CHestonModelHelper::typeid)
{
#ifdef HANDLE
	_phHestonModelHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppHestonModelHelper = new boost::shared_ptr<QuantLib::HestonModelHelper> (copy->GetShared());
        _ppCalibrationHelper = new boost::shared_ptr<QuantLib::CalibrationHelper> (boost::dynamic_pointer_cast<QuantLib::CalibrationHelper> (*_ppHestonModelHelper));
    }
}
Cephei::QL::Models::Equity::CHestonModelHelper::CHestonModelHelper (System::Type^ t) : CCalibrationHelper(CHestonModelHelper::typeid)
{
#ifdef HANDLE
	_phHestonModelHelper = NULL;
#endif
	if (!t->IsSubclassOf(CHestonModelHelper::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Equity::CHestonModelHelper::CHestonModelHelper (QuantLib::Handle<QuantLib::HestonModelHelper>& childNative, Object^ owner)  : CCalibrationHelper(CHestonModelHelper::typeid)
{
	_phHestonModelHelper = &childNative;
	_ppHestonModelHelper = &static_cast<boost::shared_ptr<QuantLib::HestonModelHelper>>(childNative.currentLink());
    _ppCalibrationHelper = new boost::shared_ptr<QuantLib::CalibrationHelper> (boost::dynamic_pointer_cast<QuantLib::CalibrationHelper> (*_ppHestonModelHelper));
    _HestonModelHelperOwner = owner;
}
Cephei::QL::Models::Equity::CHestonModelHelper::CHestonModelHelper (QuantLib::Handle<QuantLib::HestonModelHelper> childNative)  : CCalibrationHelper(CHestonModelHelper::typeid)
{
	_phHestonModelHelper = &childNative;
	_ppHestonModelHelper = &static_cast<boost::shared_ptr<QuantLib::HestonModelHelper>>(childNative.currentLink());
    _ppCalibrationHelper = new boost::shared_ptr<QuantLib::CalibrationHelper> (boost::dynamic_pointer_cast<QuantLib::CalibrationHelper> (*_ppHestonModelHelper));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Equity::CHestonModelHelper::CHestonModelHelper (QuantLib::HestonModelHelper childNative)  : CCalibrationHelper(CHestonModelHelper::typeid)
{
#ifdef HANDLE
	_phHestonModelHelper = NULL;
#endif
	_ppHestonModelHelper = new boost::shared_ptr<QuantLib::HestonModelHelper> (new QuantLib::HestonModelHelper (childNative));
    _ppCalibrationHelper = new boost::shared_ptr<QuantLib::CalibrationHelper> (boost::dynamic_pointer_cast<QuantLib::CalibrationHelper> (*_ppHestonModelHelper));
}
#endif

Cephei::QL::Models::Equity::CHestonModelHelper::~CHestonModelHelper ()
{
    if (_ppHestonModelHelper != NULL)
    {
	    delete _ppHestonModelHelper;
        _ppHestonModelHelper = NULL;
    }
}
Cephei::QL::Models::Equity::CHestonModelHelper::!CHestonModelHelper ()
{
    if (_ppHestonModelHelper != NULL)
    {
	    delete _ppHestonModelHelper;
    }
}
QuantLib::HestonModelHelper& Cephei::QL::Models::Equity::CHestonModelHelper::GetReference ()
{
    if (_ppHestonModelHelper == NULL) throw gcnew NativeNullException ();
	return **_ppHestonModelHelper;
}
boost::shared_ptr<QuantLib::HestonModelHelper>& Cephei::QL::Models::Equity::CHestonModelHelper::GetShared ()
{
    if (_ppHestonModelHelper == NULL) throw gcnew NativeNullException ();
	return *_ppHestonModelHelper;
}
QuantLib::HestonModelHelper* Cephei::QL::Models::Equity::CHestonModelHelper::GetPointer ()
{
    if (_ppHestonModelHelper == NULL) throw gcnew NativeNullException ();
	return &**_ppHestonModelHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::HestonModelHelper>& Cephei::QL::Models::Equity::CHestonModelHelper::GetHandle ()
{
	if (_phHestonModelHelper == NULL)
	{
		_phHestonModelHelper = new Handle<QuantLib::HestonModelHelper> (*_ppHestonModelHelper);
	}
	return *_phHestonModelHelper;
}
#endif
bool Cephei::QL::Models::Equity::CHestonModelHelper::HasNative () 
{
	return (_ppHestonModelHelper != NULL);
}

Double Cephei::QL::Models::Equity::CHestonModelHelper::BlackPrice (Double volatility)
{
    try
    {
        QuantLib::Real _volatility = (QuantLib::Real)ValueHelper::Convert (volatility);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHestonModelHelper)->blackPrice ( _volatility );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Equity::CHestonModelHelper::Maturity::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppHestonModelHelper)->maturity ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Equity::CHestonModelHelper::ModelValue::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHestonModelHelper)->modelValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Equity::IHestonModelHelper^ Cephei::QL::Models::Equity::CHestonModelHelper_Factory::Create (Cephei::QL::Times::IPeriod^ maturity, Cephei::QL::Times::ICalendar^ calendar, Double s0, Double strikePrice, Cephei::QL::IQuote^ volatility, Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeRate, Cephei::QL::Termstructures::IYieldTermStructure^ dividendYield, Microsoft::FSharp::Core::FSharpOption<QL::Models::CalibrationHelper::CalibrationErrorTypeEnum>^ errorType, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CHestonModelHelper ( maturity,  calendar,  s0,  strikePrice,  volatility,  riskFreeRate,  dividendYield,  errorType,  QL_Pricer);
}
